{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16212c74-9d96-4169-8b21-66c9e8886dc3",
   "metadata": {},
   "outputs": [],
   "source": [
    "#字符串操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a780bc0f-900a-44d7-b228-6fc92f10c8ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "单引号：'允许包含有\"双\"引号' 单 包 双\n",
    "双引号：\"允许包含有'单'引号\" 双 包 单\n",
    "三重引号：'''三重引号'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cff55691-7f93-4de4-8a89-b6ecdcfee90f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# coding: UTF-8\n",
    "str1 = 'I told my friend,\"Python!\"'   # 单包双\n",
    "str2 = \"'Python' is named after Monty Python, not \"  # 双包单\n",
    "str3 = \"\"\"One\"\"\"  # 三重引号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a27c7581-ed57-48bd-bc9e-58343071a72f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "199\n",
      "健康的体魄\n",
      "Jacey\n",
      "Jacey最擅长的是编程算法\n",
      "拿高薪\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "# 初始化字符串\n",
    "# 在Python中使用内置函数来定义字符串\n",
    "safe = str('健康的体魄')\n",
    "name = 'Jacey'\n",
    "message = name + '最擅长的是编程算法'\n",
    "info = '拿高薪'\n",
    "result = \"1\" + \"99\"\n",
    "\n",
    "print(result)\n",
    "\n",
    "print(safe)\n",
    "print(name)\n",
    "print(message)\n",
    "print(info)\n",
    "\n",
    "print(type(info))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b482192f-f1fc-4510-89c0-31ea3fdb7b85",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "                 IT开发\n",
      "                 TEST　PYTHON\n",
      "                 \n"
     ]
    }
   ],
   "source": [
    "# 预处理格式\n",
    "# coding:UTF-8\n",
    "information = \"学习Python, 努力前进！输出'ancode'\"\n",
    "newinformation = \"\"\"\n",
    "                 IT开发\n",
    "                 TEST　PYTHON\n",
    "                 \"\"\"\n",
    "print(newinformation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "545fb20e-638f-4da1-8e35-0e88869f1ecb",
   "metadata": {},
   "outputs": [],
   "source": [
    "infoMsg = \"编程能力： \\   # \\表示换行符 不影响程序输出\n",
    "                "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35f70474-bd80-4d0d-9d9e-87660053792c",
   "metadata": {},
   "source": [
    "### input函数的用法\n",
    "所谓'input'内置函数指的是及时可以跟"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68cf6fa4-cb01-43b5-b589-e4914fe911e1",
   "metadata": {},
   "source": [
    "字符串的填充操作\n",
    "\n",
    "center()\n",
    "rjust()\n",
    "ljust()\n",
    "zfill()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "956e6523-024d-48da-9c48-f91cf7368145",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Python**************'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "strs = \"Python\"\n",
    "strs_result1=strs.ljust(20,\"*\")\n",
    "strs_result1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9398a133-b6e2-4012-881a-8750793bb7cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'**************Python'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "strs_result2=strs.rjust(20,\"*\")\n",
    "strs_result2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "210a31ca-9d7f-402e-ab19-8b26e4254671",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'000000000000000000000000000000000000000Python'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "strs_result3=strs.zfill(45)\n",
    "strs_result3\n",
    "# 使用zfill填充菱形 思考"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a58c62d-92a5-4d6a-a800-7242f3a1e910",
   "metadata": {},
   "source": [
    "### 字符串的探索操作 <br/>\n",
    "- find()\n",
    "- count()\n",
    "- rfind()\n",
    "- startswith()\n",
    "- index()\n",
    "- endswith()\n",
    "- rindex()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "ff774317-12c8-4c21-9ab1-1ef86c3488f4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n",
      "10\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "strs_infomsg = 'Python is best language, the key is blue'\n",
    "result_start = strs_infomsg.startswith('Python')   #判断以上字符串是否以Python进行开头\n",
    "print(result_start)\n",
    "result_ends = strs_infomsg.endswith('blue')   #判断以上字符串是否以blue进行结尾\n",
    "print(result_ends)\n",
    "# 查找字符串\n",
    "result_find = strs_infomsg.find('best')  #查找best在上面的字附中是否存在\n",
    "# 找到best在上面字符串中的索引位置，如果没有找到则返回-1\n",
    "print(result_find)\n",
    "result_find2 = strs_infomsg.find(' ') # 查找空格在以上字符中的位置\n",
    "print(result_find2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5df60fcb-be34-436a-a87c-b795356e9489",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n"
     ]
    }
   ],
   "source": [
    "# index()\n",
    "result_index = strs_infomsg.index('best') # 查找best是否存在，如果没有找到则直接报错\n",
    "print(result_index)       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2daa80e3-41a1-49f5-b9a9-e2f545f81280",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n"
     ]
    }
   ],
   "source": [
    "# index()\n",
    "result_index = strs_infomsg.rindex('best') # 从右向左进行查找\n",
    "print(result_index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f976223b-e06a-4aaf-a16a-dad7b93e1bf0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "# count() 统计\n",
    "result_count1 = strs_infomsg.count(\"i\") # 统计字符i在字符串中出现的次数\n",
    "print(result_count1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "16720806-6850-4992-9555-078cb1bcd9b7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "41\n"
     ]
    }
   ],
   "source": [
    "result_count1 = strs_infomsg.count(\"\") # 如果不写任何操作，则count函数会统计当前字符串中的所有字符个数\n",
    "print(result_count1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d862fac3-cc30-442a-814e-a71a9676125c",
   "metadata": {},
   "source": [
    "### 字符串中的is.xx判断操作 <br/>\n",
    "-str.isalnum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "570ced93-ca74-4d55-b2ae-0cbc3dc64846",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unbound method str.isalnum() needs an argument",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[14], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m strs\u001b[38;5;241m.\u001b[39misalnum \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mstr\u001b[39m\u001b[38;5;241m.\u001b[39misalnum()\n\u001b[0;32m      2\u001b[0m \u001b[38;5;28mprint\u001b[39m(strs\u001b[38;5;241m.\u001b[39misalnum)\n",
      "\u001b[1;31mTypeError\u001b[0m: unbound method str.isalnum() needs an argument"
     ]
    }
   ],
   "source": [
    "strs.isalnum = str.isalnum()\n",
    "print(strs.isalnum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "0fee158f-f3ac-4061-91a7-359fc24cd934",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'strs_is' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[13], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m strs_is\n\u001b[0;32m      2\u001b[0m strs_is\n\u001b[0;32m      3\u001b[0m \u001b[38;5;28mprint\u001b[39m(strs_iss_result)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'strs_is' is not defined"
     ]
    }
   ],
   "source": [
    "strs_is\n",
    "strs_is\n",
    "print(strs_iss_result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a24fdae7-1d02-420d-bdd7-8f6d2ffb6f0e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
